// views/index/shop/certificate/gongzhengdetail/index.js
var BASE_URL = getApp().globalData.BASE_URL
var util = require('../../../../../utils/util');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    option1: [{
        text: '一月',
        value: '一月'
      },
      {
        text: '二月',
        value: '二月'
      },
      {
        text: '三月',
        value: '三月'
      },
      {
        text: '四月',
        value: '四月'
      }, {
        text: '五月',
        value: '五月'
      }, {
        text: '六月',
        value: '六月'
      }, {
        text: '七月',
        value: '七月'
      }, {
        text: '八月',
        value: '八月'
      }, {
        text: '九月',
        value: '九月'
      }, {
        text: '十月',
        value: '十月'
      },
      {
        text: '十一月',
        value: '十一月'
      }, {
        text: '十二月',
        value: '十二月'
      },
    ],
    iflogin: false, //判断是否登录
    value1: '一月',
    value: '',
    date: '一',
    ccie: '', //公证
    show: true, //判断是首次公证还是加做
    type: `首次公证`, //公证类型
    langue: '英语', //公证语种
    cate: "material", //判断命名锚计
    imglist: '', //预览图片的url列表
    price: 220, //总价
    Price: 220, //固定价格
    amount: 0, //页面中的步进器
    flag: false, //判断购物车的出现
    userid: '', //用户的id,
    shoppingcardata: '', //购物车数据
    iconnum: 0, //购物车右上数字
    last_price: 0, //总价钱



    opendiqupop: false,
    columns: ['一学期', '二学期', '三学期', '四学期', '五学期', '六学期', '七学期', '八学期', '九学期', '十学期'],
    id: '',
    xueqishu: "",
    xueqiprice: 0
  },


  opendiqupop: function () {
    this.setData({
      opendiqupop: true
    })
  },

  Closepop: function () {
    this.setData({
      opendiqupop: false
    })
  },
  onConfirm: function (e) {
    console.log(e)
    this.setData({
      opendiqupop: false,
      xueqishu: e.detail.value,
    })
    if (e.detail.value == '一学期') {

    } 
    if (e.detail.value == '二学期') {

    }
    switch (e.detail.value) {
      case '一学期':
        this.setData({
          xueqiprice: 0
        });
        break;
      case '二学期':
        this.setData({
          xueqiprice: 0
        });
        break;
      case '三学期':
        this.setData({
          xueqiprice: 10
        });
        break;
      case '四学期':
        this.setData({
          xueqiprice: 20
        });
        break;
      case '五学期':
        this.setData({
          xueqiprice: 30
        });
        break;
      case '六学期':
        this.setData({
          xueqiprice: 40
        });
        break;
      case '七学期':
        this.setData({
          xueqiprice: 50
        });
        break;
      case '八学期':
        this.setData({
          xueqiprice: 60
        });
        break;
      case '九学期':
        this.setData({
          xueqiprice: 70
        });
        break;
      default:
        this.setData({
          xueqiprice: 80
        });
        break;
    }
    this.setData({
      opendiqupop: false,
      xueqishu: e.detail.value,
      price:this.data.Price+this.data.xueqiprice
    })
  },






  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      id: options.id
    })
    var id = options.id
    console.log(options)
    var _this = this
    wx.request({
      url: `${BASE_URL}/gongzheng/index`,
      method: "POST",
      data: {
        id: id
      },
      header: {
        'Content-Type': 'application/json' //GET方式是这个
      },
      success(res) {
        console.log(res)
        res.data.wei = `${BASE_URL}` + res.data.wei
        res.data.shen = `${BASE_URL}` + res.data.shen
        res.data.zheng.forEach((item, index) => {
          if (item.length > 100) {
            res.data.zheng[index] = ''
          } else {
            res.data.zheng[index] = `${BASE_URL}` + item
          }
        })
        console.log(res)
        _this.setData({
          ccie: res.data,
          imglist: res.data.zheng
        })
      }
    })

  },

  //监听页面变化  //判断是否登录
  onShow: function () {
    var _this = this
    if (util.iflogin()) {
      var userinfo = wx.getStorageSync("USER")
      this.setData({
        iflogin: true,
        userid: userinfo.id
      })
      _this.getshoppingdata()
    }
  },


  //获取购物车数据
  getshoppingdata: function () {
    var _this = this
    wx.request({
      url: `${BASE_URL}/gongzheng/gouwuche_list`,
      method: "POST",
      data: {
        pid: _this.data.userid,
      },
      header: {
        'Content-Type': 'application/json' //GET方式是这个
      },
      success(res) {
        console.log(res)
        var str = []
        if (res.data.status !== 0) {
          res.data.info.forEach((item, index) => {
            if (item.jiazuo == 1) {
              str.push(item.fu * 25+item.xue)
            } else {
              str.push(item.fu * 25 + item.language+item.xue)
            }
          })
          var arr = str.reduce(function (a, b) {
            return a + b;
          });
          if (res.data.jiazuo !== 0) {
            arr = arr + 60
          }
          _this.setData({
            shoppingcardata: res.data,
            iconnum: res.data.num,
            last_price: arr
          })


        } else {
          _this.setData({
            shoppingcardata: '',
            iconnum: 0,
            last_price: 0
          })
        }

      }
    })
  },

  //第一个多个选项
  handleNavfirst: function (e) {
    if (e.target.dataset.type == "加做副本") {
      this.setData({
        amount: 1,
        show: false,
        price: 85,
        Price: 60
      })
    } else {
      this.setData({
        show: true,
        price: 220+this.data.xueqiprice,
        Price: 220,
        amount: 0
      })
    }
    this.setData({
      type: e.target.dataset.type
    })
  },
  //第二个多个选项
  handleNavtwo: function (e) {
    if (e.target.dataset.langue == '德语') {
      this.setData({
        price: this.data.amount * 25 + 240+this.data.xueqiprice,
        Price: 240
      })
    } else {
      this.setData({
        price: this.data.amount * 25 + 220+this.data.xueqiprice,
        Price: 220
      })
    }
    this.setData({
      langue: e.target.dataset.langue
    })
  },
  //页面中的步进器按钮绑定事件
  onchange: function (e) {
    this.setData({
      amount: e.detail,
      price: e.detail * 25 + this.data.Price+this.data.xueqiprice
    })
  },
  //点击禁止的按钮
  soucibtn: function () {
    wx.showToast({
      title: '商品数量最少为0',
      icon: 'none',
      duration: 1000
    })
  },
  jiazuobtn: function () {
    wx.showToast({
      title: '商品数量最少为1',
      icon: 'none',
      duration: 1000
    })
  },

  //公证书号 输入框
  input_value: function (e) {
    this.setData({
      value: e.detail
    })
  },
  //加做副本 首次公证时间
  onToggle: function (e) {
    this.setData({
      date: e.detail.substr(0, 1)
    })
  },
  open: function (e) {

  },


  //登录事件
  tologin: function () {
    wx.navigateTo({
      url: `/views/login/index`
    })
  },



  //加入购物车
  joinshopcar: function () {
    var _this = this
    if (_this.data.type == '首次公证') {
      wx.request({
        url: `${BASE_URL}/gongzheng/gouwuche`,
        method: "POST",
        data: {
          pid: _this.data.userid,
          jiazuo: '',
          yuyan: _this.data.langue,
          language: _this.data.Price,
          fu: _this.data.amount,
          name: _this.data.ccie.name,
          semester: _this.data.xueqishu,
          xue: _this.data.xueqiprice
        },
        header: {
          'Content-Type': 'application/json' //GET方式是这个
        },
        success(res) {
          wx.showToast({
            title: '添加购物车成功',
            icon: 'success',
            duration: 1000
          })
          console.log(res)
          _this.setData({})
          _this.getshoppingdata()
        }
      })
    } else {
      console.log(_this.data.userid, _this.data.date, _this.data.value, _this.data.amount, _this.data.ccie.name)
      wx.request({
        url: `${BASE_URL}/gongzheng/gouwuche`,
        method: "POST",
        data: {
          pid: _this.data.userid,
          jiazuo: 1,
          date: _this.data.date,
          yuyan: _this.data.value,
          fu: _this.data.amount,
          name: _this.data.ccie.name,
        },
        header: {
          'Content-Type': 'application/json' //GET方式是这个
        },
        success(res) {
          wx.showToast({
            title: '添加购物车成功',
            icon: 'success',
            duration: 1000
          })
          console.log(res)
          _this.setData({})
          _this.getshoppingdata()
        }
      })
    }

  },


  //购物车中的步进器绑定事件
  car_change: function (e) {
    var _this = this
    if (_this.data.shoppingcardata.info) {
      _this.data.shoppingcardata.info.forEach((item, index) => {
        console.log(item)
        if (item.id == e.currentTarget.dataset.id) {
          item.fu = e.detail
        }
      })
    }
    var str = []
    _this.data.shoppingcardata.info.forEach((item, index) => {
        if (item.jiazuo == 1) {
          str.push(item.fu * 25+item.xue)
        } else {
          str.push(item.fu * 25 + item.language+item.xue)
        }
    })
    var arr = str.reduce(function (a, b) {
      return a + b;
    });
    if (_this.data.shoppingcardata.jiazuo !== 0) {
      arr = arr + 60
    }
    _this.setData({
      shoppingcardata: _this.data.shoppingcardata,
      last_price: arr
    })

    console.log(e, _this.data.shoppingcardata.info)
  },

  //删除公证
  delgongzheng: function (e) {
    var _this = this
    console.log(e)
    wx.showModal({
      title: '提示',
      content: '确定删除此订单吗?',
      success(res) {
        if (res.confirm) {
          console.log('用户点击确定')
          wx.request({
            url: `${BASE_URL}/gongzheng/gz_del.html`,
            method: "GET",
            data: {
              id: e.currentTarget.dataset.id
            },
            header: {
              'Content-Type': 'application/json' //GET方式是这个
            },
            success(res) {
              console.log(res)
              _this.getshoppingdata()
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
















  //购物车清空
  closesky: function () {
    var _this = this
    wx.showModal({
      title: '提示',
      content: '确定要清空订单列表吗?',
      success(res) {
        if (res.confirm) {
          wx.request({
            url: `${BASE_URL}/gongzheng/listqingkong`,
            method: "GET",
            data: {
              id: _this.data.userid
            },
            header: {
              'Content-Type': 'application/json' //GET方式是这个
            },
            success(res) {
              console.log(res)
              _this.getshoppingdata()
            }
          })

          console.log('用户点击确定')
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
















  //去支付
  gopaymoney: function () {
    console.log('你去了支付页面')
    wx.navigateTo({
      url: `../paypage/index`
    })

  },
















  //命名描记
  jump: function (e) {
    console.log(e.currentTarget.dataset.cate)
    this.setData({
      cate: e.currentTarget.dataset.cate
    })
    var me = this;
    var query = wx.createSelectorQuery().in(me);
    query.selectViewport().scrollOffset()
    //#comm 跳转到指定id位置
    query.select(`#${e.currentTarget.dataset.cate}`).boundingClientRect();
    query.exec(function (res) {
      console.log(res)
      var miss = res[0].scrollTop + res[1].top - 80;
      wx.pageScrollTo({
        scrollTop: miss,
        duration: 500
      });
    });
  },

  //购物车出现
  showPopup: function () {
    this.setData({
      flag: !this.data.flag
    })
  },
  // 购物车消失
  onClose() {
    this.setData({
      flag: false
    });
  },
























  //图片预览
  preview_shen: function (e) {
    var _this = this;
    var url = e.currentTarget.dataset.url;
    wx.previewImage({
      current: url, // 当前显示图片的http链接
      urls: [_this.data.ccie.shen] // 需要预览的图片http链接列表
    })
  },

  preview_zheng: function (e) {
    var _this = this;
    var url = e.currentTarget.dataset.url;
    console.log(url)
    if (url) {
      wx.previewImage({
        current: url, // 当前显示图片的http链接
        urls: _this.data.imglist // 需要预览的图片http链接列表
      })
    }
  },


  preview_wei: function (e) {
    var _this = this;
    var url = e.currentTarget.dataset.url;
    wx.previewImage({
      current: url, // 当前显示图片的http链接
      urls: [_this.data.ccie.wei] // 需要预览的图片http链接列表
    })
  },
})